using System.Collections.Generic;
using FlowCanvas.Nodes;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gta.Common;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class StreetFightReborn : CallableActionNode
    {
        private List<WaitRebornInfo> waitRebornNpcList = new List<WaitRebornInfo>(16);

        private List<WaitRebornInfo> bornInvincibleNpcList = new List<WaitRebornInfo>(16);

        // 新成员变量，存储每个复活点的最后使用时间
        private Dictionary<Transform, float> lastUsedRebornPoints = new Dictionary<Transform, float>();
        List<Transform> availableBornPoints  = new List<Transform>();

        public override void Invoke()
        {
            var deadNpcList = StreetFightEvent.Instance.DeadNpcList;
            if (deadNpcList != null && deadNpcList.Count > 0)
            {
                foreach (var deadNpcId in deadNpcList)
                {
                    waitRebornNpcList.Add(deadNpcId);
                }

                deadNpcList.Clear();
            }

            if (waitRebornNpcList.Count > 0)
            {
                for (int i = waitRebornNpcList.Count - 1; i >= 0; i--)
                {
                    var waitRebornInfo = waitRebornNpcList[i];
                    var info = waitRebornInfo;
                    if (Time.time - info.startRebornTime > StreetFightEvent.Instance.RebornTime)
                    {
                        availableBornPoints.Clear();
                        // 创建符合条件的复活点列表
                        foreach (var bornTrans in StreetFightEvent.Instance.BornPointList)
                        {
                            if (!lastUsedRebornPoints.ContainsKey(bornTrans) ||
                                Time.time - lastUsedRebornPoints[bornTrans] > 3f)
                            {
                                availableBornPoints.Add(bornTrans);
                            }
                        }

                        // 从列表中随机选择一个复活点
                        if (availableBornPoints.Count > 0)
                        {
                            int bornPointListIndex = UnityEngine.Random.Range(0, availableBornPoints.Count);
                            var selectedBornTrans = availableBornPoints[bornPointListIndex];

                            // 确保找到了可用的复活点
                            if (selectedBornTrans != null)
                            {
                                if (info.id == LibertyAIUtils.PLAYER_AI_ID)
                                {
                                    LibertyAIUtils.PlayerController.RebornPlayer();
                                    LibertyAIUtils.PlayerController.PlayerTransmission(
                                        selectedBornTrans.position,
                                        selectedBornTrans.eulerAngles,
                                        true, true);
                                    LibertyAIUtils.PlayerController.playerEntity.AIEntity.data.invincible = true;
                                    LibertyAIUtils.PlayerController.playerEntity.AIEntity.battleAgent.StartFlashing();
                                }
                                else
                                {
                                    AIBirthData birthData = new AIBirthData();
                                    birthData.uniqueId = info.id;
                                    birthData.position = selectedBornTrans.position;
                                    birthData.forward = selectedBornTrans.forward;
                                    birthData.modelResId = info.modelId;
                                    birthData.dropItemPackId = info.dropItemPackId;
                                    birthData.rebornPoint = info.rebornPoint;
                                    birthData.animState = "";
                                    birthData.sideType = SideType.Madness;
                                    birthData.isBoss = false;
                                    birthData.isStreetEvent = true;
                                    birthData.isStatic = false;
                                    LibertyAIUtils.AIManager.AddNpc(birthData, 1003);
                                    //设置无敌
                                    var npc = LibertyAIUtils.AIManager.NpcBirthManager.GetNpc(info.id);
                                    if (npc!=null)
                                    {
                                        npc.data.invincible = true;
                                        //无敌特效
                                        npc.battleAgent.StartFlashing();
                                    }
                                }

                                waitRebornInfo.reBornTime = Time.time;
                                bornInvincibleNpcList.Add(waitRebornInfo);
                                //复活
                                waitRebornNpcList.RemoveAt(i);

                                lastUsedRebornPoints[selectedBornTrans] = Time.time;
                            }
                        }
                    }
                }
            }

            if (bornInvincibleNpcList.Count > 0)
            {
                for (int i = bornInvincibleNpcList.Count - 1; i >= 0; i--)
                {
                    var bornInfo = bornInvincibleNpcList[i];
                    if (Time.time - bornInfo.reBornTime > StreetFightEvent.Instance.DaddyTime)
                    {
                        if (bornInfo.id == LibertyAIUtils.PLAYER_AI_ID)
                        {
                            LibertyAIUtils.PlayerController.playerEntity.AIEntity.data.invincible = false;
                        }
                        else
                        {
                            var npc = LibertyAIUtils.AIManager.NpcBirthManager.GetNpc(bornInfo.id);
                            if (npc!=null)
                            {
                                npc.data.invincible = false;
                            }
                        }

                        bornInvincibleNpcList.RemoveAt(i);
                    }
                }
            }
        }
    }
}